package pl.edu.agh.gt.data;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Date;

public class DataProvider {

	@SuppressWarnings("deprecation")
	public static RatesHistory readData(File csv) throws IOException {

		RatesHistory rates = new RatesHistory("EURUSD");

		BufferedReader reader = new BufferedReader(new FileReader(csv));
		String line;

		while ((line = reader.readLine()) != null) {
			String[] fields = line.split(",");
			Date date = new Date();
			date.setYear(Integer.valueOf(fields[1].substring(0, 4)) - 1900);
			date.setMonth(Integer.valueOf(fields[1].substring(4, 6)) - 1);
			date.setDate(Integer.valueOf(fields[1].substring(6, 8)));
			String[] time = fields[2].split(":");
			date.setHours(Integer.valueOf(time[0]));
			date.setMinutes(Integer.valueOf(time[1]));
			date.setSeconds(Integer.valueOf(time[2]));

			Rate rate = new Rate(
					Float.valueOf(fields[3]),
					Float.valueOf(fields[4]), 
					Float.valueOf(fields[5]),
					Float.valueOf(fields[6]), 
					date);

			rates.addNextRate(rate);
		}

		return rates;
	}
}
